<?php
/**
 * Created by PhpStorm.
 * User: Wang YuLong
 * Date: 2018/11/7
 * Time: 16:20
 * desc:商品分类
 */
namespace app\index\controller;

use think\Controller;
use app\index\model\Classify;

class Classifys extends Comm
{
    /**
     * @return \think\response\View
     * @throws \think\exception\DbException
     * 商品分类的首页
     */
    public function index()
    {
        $data = Classify::all()->toArray();
        $this->assign('data',$data);
        return view('index');
    }

    /**
     * 添加商品分类
     * @return \think\response\View
     */
    public function add()
    {

        if (isset($_POST['submit']))
        {
            $classify           = new Classify();
            $name = trim(input('name'));
            if(!empty($name))
            {
                $data = Classify::all()->column('sort');
                $sort = Max($data);
                $classify->name     = input('name');
                $classify->sort        = $sort+1;
                if($classify->allowField(true)->save()==true)
                {
                    zsgc('添加了一个新的商品分类，名称为'.input('name'));
                    $this->success('操作成功','index/classifys/index');
                }
                else
                {
                    $this->error('操作失败','index/classifys/index');
                }
            }
            else
            {
                $this->error('123操作失败','index/classifys/index');
            }
        }else
        {
            return view('add');
        }
    }

    /**
     * @throws \think\exception\DbException
     * 删除商品分类
     */
    public function del()
    {
        $id = trim(input('id'));
        if(!empty($id))
        {
            $data = Classify::get($id);
            if($data)
            {
                zsgc('删除了一个商品分类，名称为'.$data['name']);
                $data->delete();
                $this->success('操作成功','index/classifys/index');
            }
            else
            {
                $this->error('操作失败','index/classifys/index');
            }
        }
        else
        {
            $this->error('操作失败','index/classifys/index');
        }
    }

    /**
     * 修改商品分类
     * @throws \think\exception\DbException
     */
    public function mod()
    {
        if(isset($_POST['submit']))
        {
            $id = trim(input('id'));
            if(!empty($id))
            {
                $name = trim(input('name'));
                $sort    = trim(input('sort'));
                $classify = new Classify();
                $classify->save([
                    'name'  => $name,
                    'sort' => $sort
                ],['id' => $id]);
                zsgc('修改了一个商品分类，修改后的名称为'.$name);
                $this->success('操作成功','index/classifys/index');
            }else
            {
                $this->error('操作失败','index/classifys/index');
            }
        }else{
            $id = trim(input('id'));
            $data = Classify::get($id);
            $this->assign('data',$data);
            return view('mod');
        }
    }
}
